package com.example.demo.service.impl;

import com.example.demo.entity.QuestionCategory;
import com.example.demo.entity.User;
import com.example.demo.exception.LoginException;
import com.example.demo.exception.ServiceException;
import com.example.demo.mapper.ICategoryMapper;
import com.example.demo.service.ICateGoryService;
import com.example.demo.service.ITokenService;
import com.example.demo.util.Judge;
import com.example.demo.util.imgUtil.IImgService.IFileImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl implements ICateGoryService {

    @Autowired private ICategoryMapper categoryMapper;
    @Autowired private IFileImgService fileImgService;
    @Value("${imglocal}") private String imglocal;

    @Override
    public Boolean insert(String title, String img, HttpServletRequest request) throws ServiceException{
        if (Judge.isNull(title)){
            throw new ServiceException("请完善必填项！");
        }
        img = this.fileImgService.addImgofficialUpload(img, imglocal, request);
        Integer affect = this.categoryMapper.insert(Judge.getUUID(),title, img,0,new Date());
        if (affect != 1){
            throw new ServiceException("操作失败！");
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean delete(String id) throws ServiceException{
        if (Judge.isNull(id)){
            throw new ServiceException("请提供有效ID！");
        }
        Integer affect = this.categoryMapper.delete(id,1);
        if (affect != 1){
            throw new ServiceException("删除失败！");
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean update(String id, String title, String img, HttpServletRequest request) {
        if (Judge.isNull(id)){
            throw new ServiceException("请提供有效ID！");
        }
        QuestionCategory category = this.categoryMapper.getById(id);
        if(category == null){
            throw new ServiceException("请传输有效id");
        }
        if(Judge.isNotNull(title)){
            category.setTitle(title);
        }
        if(Judge.isNotNull(img)){
            img = this.fileImgService.addImgofficialUpload(img, imglocal, request);
            category.setImg(img);
        }
        if (Judge.isNotNull(title)){
            Integer affect = this.categoryMapper.update(category.getId(), category.getTitle(), category.getImg());
            if (affect != 1){
                throw new ServiceException("修改失败！");
            }
        }
        return Boolean.TRUE;
    }

    @Override
    public QuestionCategory getById(String id) {
        if (Judge.isNull(id)){
            throw new ServiceException("请提供有效ID！");
        }
        QuestionCategory questionCategory = this.categoryMapper.getById(id);
        return questionCategory;
    }

    @Override
    public List<QuestionCategory> getAll() {
        List<QuestionCategory> questionCategoryList = this.categoryMapper.getAll();
        return questionCategoryList;
    }
}
